package com.example.server.service.admin.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.server.common.constant.Result;
import com.example.server.entity.Customer;
import com.example.server.entity.Sword;
import com.example.server.mapper.SwordMapper;
import com.example.server.service.admin.SwordService;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

/**
 * @author china.fuyao@outlook.com
 * @date 2020-04-20 17:19
 */
@Service("swordService")
public class SwordServiceImpl extends ServiceImpl<SwordMapper, Sword> implements SwordService {
    /**
     * 新增敏感词
     *
     * @param sword
     * @param customerId
     * @return
     */
    @Override
    public Result addSword(String sword, Integer customerId) {
        Sword one = getOne(new QueryWrapper<Sword>().eq("sword", sword));
        if (null == one) {
            Sword s = new Sword();
            s.setSword(sword);
            boolean save = save(s);
            if (save) {
                return Result.OK();
            }
            return Result.Error();
        }
        return Result.Error("敏感词已存在");
    }

    /**
     * 更新敏感词
     *
     * @param swordId
     * @param sword
     * @param customerId
     * @return
     */
    @Override
    public Result updateSword(Integer swordId, String sword, Integer customerId) {
        Sword one = getOne(new QueryWrapper<Sword>().eq("sword", sword));
        if (null == one) {
            Sword s = getById(swordId);
            s.setSword(sword);
            boolean update = updateById(s);
            if (update) {
                return Result.OK();
            }
            return Result.Error();
        }
        return Result.Error("敏感词重复");
    }

    /**
     * 批量删除敏感词
     *
     * @param swordIds
     * @param customerId
     * @return
     */
    @Override
    public Result batchDelSword(String swordIds, Integer customerId) {
        if (StrUtil.isNullOrUndefined(swordIds)) {
            return Result.Error("请求参数错误");
        }
        String[] split = swordIds.split(",");
        List<String> asList = Arrays.asList(split);
        boolean remove = removeByIds(asList);
        if (remove) {
            return Result.OK();
        }
        return Result.Error();
    }

    /**
     * 敏感词列表
     *
     * @param page
     * @param limit
     * @param sword
     * @param customerId
     * @return
     */
    @Override
    public Result swordList(Integer page, Integer limit, String sword, Integer customerId) {
        IPage iPage = new Page(page, limit);
        IPage<Sword> data;
        if (StrUtil.isNullOrUndefined(sword)) {
            data = page(iPage);
        } else {
            QueryWrapper<Sword> wrapper = new QueryWrapper<Sword>().like("sword", sword);
            data = page(iPage, wrapper);
        }
        return new Result(data);
    }
}
